<?php
/**
 * 前端导航
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Page;
use Common\Library\Category;
class NavigationController extends CommonController{
	private $msg=array();
	private $Model;
	/**
	 * 初始化，创建导航模型
	 */
	function _initialize(){
		parent::_initialize();
		$this->Model=D('Navigation');
	}

	/**
	 * 列表
	 */
	public function index(){
		if(!$this->checkPermission('Navigation')) $this->jump();
		$data=array();
		if(I('title')) $data['title']=I('title');
		if(I('status')) $data['status']=I('status');
		$list=$this->Model->getNavigations($data);
		$url['p']=I('get.p','');
		$list=Category::unlimitedForLevel($list,'',0,0,'navigation_id','parent_id','title');
		$count=count($list);
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$list=array_splice($list,$Page->firstRow,$Page->listRows);
		$show=$Page->show();
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->display();
	}

	/**
	 * 操作
	 */
	public function oper(){
		$navigation_id=I('get.navigation_id',0);
		if(!$navigation_id && !$this->checkPermission('AddNavigation')) $this->jump();
		$languages=get_langs();
		$navigation=$this->Model->getNavigation($navigation_id);
		if($navigation_id && empty($navigation)) $this->jump(L('error_404'));
		if(IS_POST){
			$navigation=I('post.');
			if($this->vilidateForm()){
				$this->Model->operNavigation($navigation,$navigation_id);
				session('success',$navigation_id?L('success_edit'):L('success_add'));
				$url['p']=I('get.p','');
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$this->assign('languages',$languages);
		$data_filter=array(
			'navigation_id'	=>	$navigation_id
			);		
		$navigations=$this->Model->getNavigations($data_filter);
		$navigations=Category::unlimitedForLevel($navigations,'',0,0,'navigation_id','parent_id');
		$this->assign('navigations',$navigations);
		$this->assign('msg',$this->msg);
		$this->assign('navigation',$navigation);
		$this->display();
	}

	/**
	 * 数据验证
	 */
	private function vilidateForm(){
		$navigation_id=I('get.navigation_id',0,'trim');
		if($navigation_id && !$this->checkPermission('EditNavigation')){
			$this->msg['warning']=L('error_not_permssion');
			return !$this->msg;
		}
		$data=I('post.');
		if(mb_strlen($data['href'])<3 || mb_strlen($data['href'])>255){
			$this->msg['href'] = L('error_href');
		}
		foreach($data['navigation_description'] as $language_id => $value){
			if(mb_strlen($value['title'])<2 || mb_strlen($value['title'])>50){
				$this->msg['title'][$language_id]=L('error_title');
			}
			if(mb_strlen($value['description'])>255){
				$this->msg['description'][$language_id]=L('error_description');
			}
		}
		return !$this->msg;
	}
	public function del(){
		if($this->checkPermission('DeleteNavigation')){
			$ids=I('post.selected');
			$count=0;
			foreach($ids as $navigation_id){
				$count += $this->Model->del($navigation_id);
			}
			session('success',L('success_delete',array('count'=>$count)));
		}else{
			session('danger',L('error_not_permssion'));
		}
		$this->redirect('index');
	}
}